package com.youhong.teethcare.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.youhong.teethcare.R

class BrushingStepAdapter(val titles: Array<String>, val contents: IntArray, val showTips: Boolean) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    class CommonProblemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvTilte = itemView.findViewById<TextView>(R.id.tvBrushingTitle)
        var ivContent = itemView.findViewById<ImageView>(R.id.ivBrushingContent)
        var tvContent = itemView.findViewById<TextView>(R.id.tvContent2)
    }

    class EmptyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvEmpty = itemView.findViewById<TextView>(R.id.tvSpaceBottom)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            itemViewType_Empty -> {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.item_empty_tips, parent, false);
                EmptyViewHolder(view)
            }
            else -> {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.item_brushing_step, parent, false);
                CommonProblemViewHolder(view)
            }
        }

    }

    override fun onBindViewHolder(viewholder: RecyclerView.ViewHolder, position: Int) {

        when (getItemViewType(position)) {
            itemViewType_Empty -> {
                bindEmpty(viewholder as EmptyViewHolder)
            }
            else -> {
                bindContent(viewholder as CommonProblemViewHolder,position);
            }
        }
    }
    fun bindContent(viewholder: CommonProblemViewHolder,position: Int){

        var title = titles[position]
        var id = contents[position]
        viewholder.tvTilte.text = title
        viewholder.ivContent.setImageResource(id)
        var visibility = View.GONE;
        if (position == 1 && showTips) {
            visibility = View.VISIBLE;
        }
        viewholder.tvContent.visibility = visibility;
    }
    fun bindEmpty(viewholder: EmptyViewHolder){

    }
    override fun getItemCount(): Int {
        return titles.size + 1
    }

    var itemViewType_Empty = 0;
    var itemViewType_Content = 1;
    override fun getItemViewType(position: Int): Int {
        return when (position) {
            titles.size -> itemViewType_Empty
            else -> itemViewType_Content;
        };
    }
}